package com.qifenqian.turbot.module.ui.comment

import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.module.adapter.itemdecoration.LinearItemDecoration
import com.qifenqian.turbot.module.adapter.restaurant.CustomerCommentAdapter
import com.qifenqian.turbot.module.contract.CommentContract
import com.qifenqian.turbot.module.entity.response.CommentEntity
import com.qifenqian.turbot.module.presenter.CommentPresenter
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.activity_customer_comment.*
import kotlinx.android.synthetic.main.item_customer_comment.view.*
import org.jetbrains.anko.toast

class CustomerCommentActivity : BaseActivity<CommentPresenter, CommentContract.View>(), CommentContract.View {

    companion object {
        fun startThis(context: Context) {
            val intent = Intent(context, CustomerCommentActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_customer_comment
    }

    override fun bindPresenter(): CommentPresenter? {
        return CommentPresenter(this, this)
    }

    private var mPage: Int = 1
    private var mView: View? = null
    private var mCustomerCommentAdapter: CustomerCommentAdapter? = null
    private var mCommentEntities: MutableList<CommentEntity> = mutableListOf()

    override fun initial() {
        mPresenter!!.getCommentList(1)
        srl_customer_comment.setRefreshHeader(ClassicsHeader(this))
        srl_customer_comment.setRefreshFooter(ClassicsFooter(this))
        srl_customer_comment.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                mCommentEntities.clear()
                mPresenter!!.getCommentList(1)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mPage++
                mPresenter!!.getCommentList(mPage)
            }
        })

        rv_customer_comment.layoutManager = LinearLayoutManager(this)
        mCustomerCommentAdapter = CustomerCommentAdapter(mCommentEntities)
        rv_customer_comment.addItemDecoration(LinearItemDecoration(this, spaceWidth = 6))
        rv_customer_comment.adapter = mCustomerCommentAdapter
        mCustomerCommentAdapter!!.setOnChildClickListener(object : CustomerCommentAdapter.OnReplyClickListener {
            override fun onClick(view: View, position: Int, replyContent: String) {
                // 2018/12/20 18:12 回复,请求网络,成功则切换布局 ----->
                mPresenter!!.reply(mCommentEntities[position].id, replyContent, mCommentEntities[position].userName)
                mView = view
            }
        })
    }

    override fun getCommentListResult(isSuccess: Boolean, code: Int, msg: String?, commentEntities: List<CommentEntity>?) {
        if (isSuccess) {
            mCommentEntities.clear()
            mCommentEntities.addAll(commentEntities as MutableList<CommentEntity>)
            srl_customer_comment.finishRefresh()
            srl_customer_comment.finishLoadMore()
            mCustomerCommentAdapter!!.notifyDataSetChanged()
        } else {
            toast(msg!!)
        }
    }

    override fun replyResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            mView!!.btn_item_customer_comment_reply.visibility = View.GONE
            mView!!.ly_item_customer_comment_reply.visibility = View.GONE
            mView!!.ly_item_customer_comment_reply_result.visibility = View.VISIBLE
        } else {
            toast(msg!!)
        }
    }


}
